package com.cszjzx.member.action;

import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.cszjzx.member.po.Master;
import com.cszjzx.member.po.Member;
import com.cszjzx.member.po.Page;
import com.cszjzx.member.po.TempleMember;
import com.cszjzx.member.service.TempleMemberService;
import com.cszjzx.member.service.TempleMemberServiceImpl;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class AuditAction extends ActionSupport{
	private int checkcondition;
	private int masterid;
	private int beginPage=1;	//默认从第一页开始
	private int everyPage = 10;	//每页显示10个
	private Page page;			
	private int totalNums;		//求总数
	private String idcard;
	private String message;
	private TempleMember templeMember;
	private Member member;
	private int process;
	@SuppressWarnings("rawtypes")
	private List choose;
	private TempleMemberService templeMemberService = new TempleMemberServiceImpl();
	HttpServletRequest request = ServletActionContext.getRequest();
	Master master;
	private int id;		//根据templeMember表中的序号决定进行哪一条审核
	private String failReason;			//存储未通过的理由
	private String details;				//存储操作具体信息
	public String getDetails() {
		return details;
	}

	public void setDetails(String details) {
		this.details = details;
	}

	public String getFailReason() {
		return failReason;
	}

	public void setFailReason(String failReason) {
		this.failReason = failReason;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public Master getMaster() {
		return master;
	}

	public void setMaster(Master master) {
		this.master = master;
	}

	@SuppressWarnings("rawtypes")
	public List getChoose() {
		return choose;
	}

	@SuppressWarnings("rawtypes")
	public void setChoose(List choose) {
		this.choose = choose;
	}

	public int getProcess() {
		return process;
	}

	public void setProcess(int process) {
		this.process = process;
	}

	public Member getMember() {
		return member;
	}

	public void setMember(Member member) {
		this.member = member;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public TempleMember getTempleMember() {
		return templeMember;
	}

	public void setTempleMember(TempleMember templeMember) {
		this.templeMember = templeMember;
	}

	public int getBeginPage() {
		return beginPage;
	}

	public void setBeginPage(int beginPage) {
		this.beginPage = beginPage;
	}

	public int getEveryPage() {
		return everyPage;
	}

	public void setEveryPage(int everyPage) {
		this.everyPage = everyPage;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public int getTotalNums() {
		return totalNums;
	}

	public void setTotalNums(int totalNums) {
		this.totalNums = totalNums;
	}

	public int getMasterid() {
		return masterid;
	}

	public void setMasterid(int masterid) {
		this.masterid = masterid;
	}

	public int getCheckcondition() {
		return checkcondition;
	}

	public void setCheckcondition(int checkcondition) {
		this.checkcondition = checkcondition;
	}
	
	public String show(){
		//发现由superAdmin.xml文件中执行内部的chain方式跳转，传递checkcondition值不会变，因此在这儿再写一次变化
		checkcondition = 1;
		//是超级管理员
		if(masterid == 0){
			page = templeMemberService.getPage(checkcondition, beginPage);
		}
		else{
			page = templeMemberService.getPage(masterid, checkcondition, beginPage);
		}
		totalNums = page.getTotalCount();
		return "showAuditSuccess";
	}
	
	public String edit(){
		//现在需要根据id来决定进行哪一条审核
		if(templeMemberService.updateTempleMember(id, checkcondition, failReason)){
			message = "操作成功";
		}
		else {
			message = "操作不成功";
		}
		checkcondition = 1;
		return "editAuditSuccess";
	}
	
	@SuppressWarnings("rawtypes")
	public String edits(){
		message = "";
		if(choose == null){
			message = "您未选中任何一条记录";
			return "deleteSuccess";
		}
		int size = choose.size();
		Iterator iterator = choose.iterator();
		int[] ids = new int[size];
		int i = 0;
		while(iterator.hasNext()){
			String s = (String)iterator.next();
			ids[i] = Integer.parseInt(s);
			i++;
		}
		for(int j=0;j<size;j++){
			if(templeMemberService.updateTempleMember(ids[j], checkcondition, failReason)){
				message = "操作成功";
			}
			else {
				message = "操作不成功";
			}
		}
		checkcondition = 1;
		return "deleteSuccess";
	}
	
	public String showDetail(){
		templeMember = templeMemberService.getById(id);
		member = templeMember.getMember();
		if(templeMember.getCheckcondition() == 3){
			failReason = templeMember.getFailReason();
		}
		else{
			failReason = null;
		}
		process = Integer.parseInt(member.getValue("process"));
		if(checkcondition == 0){
			return "editAuditDetail";
		}
		return "showAuditDetail";
	}
	
	public String showPass(){
		//masterid值没有传过来的话，默认是0，代表是超级管理员，不是管理员
		if(masterid == 0){
			page = templeMemberService.getPage(2, beginPage);
		}
		else{
			int auditPass = (int) request.getSession().getAttribute("auditPass");
			if(auditPass != 0){
				templeMemberService.setIsReadNull(masterid);
			}
			page = templeMemberService.getPage(masterid, 2, beginPage);
		}
		totalNums = page.getTotalCount();
		return "showAuditPassSuccess";
	}
	
	public String showUnpass(){
		if(masterid == 0){
			page = templeMemberService.getPage(3, beginPage);
		}
		else{
			page = templeMemberService.getPage(masterid, 3, beginPage);
		}
		totalNums = page.getTotalCount();
		return "showAuditUnpassSuccess";
	}
	
	public String operation(){
		master = (Master)request.getSession().getAttribute("master");
		masterid = master.getId();
		if(master.isIssuper()){
			if(checkcondition==1){
				return "showAuditSuccesses";
			}
			else if(checkcondition==2){
				return "showAuditPassSuccesses";
			}
			else{
				return "showAuditUnpassSuccesses";
			}
		}else{
			if(checkcondition==0){
				return "adminAudites";
			}
			else if(checkcondition==1){
				return "showAuditSuccesses";
			}
			else if(checkcondition==2){
				return "showAuditPassSuccesses";
			}
			else{
				return "showAuditUnpassSuccesses";
			}
		}
	}

	public String getIdcard() {
		return idcard;
	}

	public void setIdcard(String idcard) {
		this.idcard = idcard;
	}
	
}
